// pages/addresseeInfo/addresseeInfo.js
import api from "../../config/api";
import request from "../../utils/request";
import { parse ,isPhoneRight} from '../../utils/utils'
import {
	shareOptions
} from "../../utils/access";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region:[],
    regionText:'',
    recipientName:'',
    recipientPhone:'',
    detailAddress:'',
    canClick:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.info){
      let info = parse(options.info)
      console.log('info',info);
      const {recipientName,recipientPhone,detailAddress,province,city,district,id} = info
      this.setData({
        recipientName,
        recipientPhone,
        detailAddress,
        id,
        region:[province,city,district],
        regionText:province+city+district
      })
    }
  },
  changeAddress:function(e){
    let val = e.detail.value
    if(val.length<31){
      this.setData({
        detailAddress:val
      })
    }
  },
  changeName:function(e){
    let val = e.detail.value
    if(val.length<16){
      this.setData({
        recipientName:val
      })
    }
  },
  changePhone:function(e){
    this.setData({
      recipientPhone:e.detail.value
    })
  },
  save:async function(){
    const {recipientName,recipientPhone,detailAddress,region,id,canClick} = this.data
    if(canClick){
      if(recipientName.replace(/\s*/g,"").length===0){
        wx.showToast({
          title: '姓名不能为空',
          icon:'none'
        })
        return
      }
      if(recipientName.replace(/\s*/g,"").length<2){
        wx.showToast({
          title: '姓名最短2个字',
          icon:'none'
        })
        return
      }
      let reg= /[^A-Za-z\u4e00-\u9fa5]+/;
      if(reg.test(recipientName)){
        wx.showToast({
          title: '姓名只能用中文或英文',
          icon: 'none'
        })
        return 
      }
      if(recipientPhone===''){
        wx.showToast({
          title: '手机号不能为空',
          icon:'none'
        })
        return
      }
      if(!isPhoneRight(recipientPhone)){
        wx.showToast({
          title: '请正确填写手机号',
          icon:'none'
        })
        return
      }
      console.log('detailAddress',detailAddress.length);
      if(detailAddress.replace(/\s*/g,"").length===0){
        wx.showToast({
          title: '详细地址不能为空',
          icon:'none'
        })
        return
      }
      if(region.length===0){
        wx.showToast({
          title: '省市区不能为空',
          icon:'none'
        })
        return
      }
      let params = {
        recipientName,
        recipientPhone,
        detailAddress,
        province:region[0],
        city:region[1],
        district:region[2],
      }
      this.setData({
        canClick:false
      })
      const res = await request.post(id?api.updateAddressee:api.createAddressee,id?{...params,id}:params)
      console.log('createAddressee',res);
      if(res.httpStatus===200){
        wx.showToast({
          title: '保存成功',
          icon:'none'
        })
        setTimeout(()=>{
          wx.navigateBack({
            delta: 1,
          })
        },1500)
      }else{
        this.setData({
          canClick:true
        })
        wx.showToast({
          title: '保存失败',
          icon:'none'
        })
      }
    }
  },
  bindRegionChange:function(e){
    let val = e.detail.value
    this.setData({
      region:val,
      regionText:val[0]+val[1]+val[2]
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return shareOptions('default',e)
  }
})